//
//  XSJLiveVC.m
//  XSJ
//
//  Created by apple on 28.4.20.
//  Copyright © 2020 apple. All rights reserved.
//

#import "XSJLiveVC.h"
#import "XSJHotEventTableCell.h"
#import "SVProgressHUD.h"
#import "MJExtension.h"
#import "RefreshGifHeader.h"
#import "TYShowAlertView.h"
#import "XSJMoreHeadView.h"
#import "XSJRecommendedCCell.h"
#import "XSJWholeCollectionCell.h"
#import "XSJAnchorViewController.h"

@interface XSJLiveVC ()<XSJMoreHeadViewDelegate,XSJHotEventTableCellDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

/* 顶部工具View */
//@property (nonatomic, strong) ZFHomeTopToolView *topToolView;

/* collectionView */
@property (strong , nonatomic)UICollectionView *collectionView;

@property (strong , nonatomic)NSMutableArray *imageUrls;
@property (strong , nonatomic)NSMutableArray *imageUrls2;

////推荐
//@property (nonatomic, strong) HotHouseListModel *listModel;
////热门
//@property (nonatomic, strong) HotHouseListModel *listModel2;
//
////轮播
//@property (nonatomic, strong) HotHouseListModel *listModel3;
//@property (nonatomic, strong) HotHouseListModel *listModel4;

@end

@implementation XSJLiveVC

/* cell */
static NSString *const XSJRecommendedCCellID = @"XSJRecommendedCCellID";
static NSString *const XSJWholeCollectionCelllD = @"XSJWholeCollectionCelllD";
/* head */
static NSString *const XSJHotEventTableCellID = @"XSJHotEventTableCellID";
static NSString *const XSJMoreHeadViewID = @"XSJMoreHeadViewID";
/* foot */
//static NSString *const ZFTopLineFootViewID = @"ZFTopLineFootViewID";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"直播";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Scroll" style:UIBarButtonItemStyleDone target:self action:@selector(gotoPage)];
    
//    for (int index = 0; index < 5; index++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",index]];
//        [self.imageUrls addObject:image];
//    }
    
    [self setupUI];
}

- (void)gotoPage {
    XSJAnchorViewController *vc = [[XSJAnchorViewController alloc] initWithPushUrl:@"rtmp://pa4dab0cd.live.126.net/live/d4922929a90843cbbef345518c1d97f8?wsSecret=79149bf70a1486efe1fc6d91e04ad2e6&wsTime=1573623751"];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}


- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.collectionView.backgroundColor = TableViewBGColor;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.collectionView.mj_header = [RefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];

//    [self.collectionView.mj_header beginRefreshing];
//    [self loadData2];
//    [self loadData3:@"top"];
//    [self loadData3:@"middle"];
}

//推荐房间
//-(void)loadData
//{
//    ZWeakSelf
//    [http_hall hallCommendRoom:@"" pageSize:10 success:^(id responseObject)
//     {
//         // 拿到当前的下拉刷新控件，结束刷新状态
//         [weakSelf.collectionView.mj_header endRefreshing];
//         // 结束刷新
//         //[weakSelf.collectionView.mj_footer endRefreshing];
//         [weakSelf loadData_success:responseObject];
//     } failure:^(NSError *error) {
//         // 拿到当前的下拉刷新控件，结束刷新状态
//         [weakSelf.collectionView.mj_header endRefreshing];
//         // 结束刷新
//         //[weakSelf.collectionView.mj_footer endRefreshing];
//         [SVProgressHUD showErrorWithStatus:error.domain];
//     }];
//}

//-(void)loadData_success:(id)responseObject
//{
//    if (kObjectIsEmpty(responseObject))
//    {
//        return;
//    }
//    self.listModel.list = [HotHouseModel mj_objectArrayWithKeyValuesArray:responseObject];
//    [self.collectionView reloadData];
//}

//热门房间
//-(void)loadData2
//{
//    ZWeakSelf
//    [http_hall hallHotRoom:^(id responseObject)
//    {
//        // 拿到当前的下拉刷新控件，结束刷新状态
//        [weakSelf.collectionView.mj_header endRefreshing];
//        // 结束刷新
//        //[weakSelf.collectionView.mj_footer endRefreshing];
//        [weakSelf showData:responseObject];
//    } failure:^(NSError *error) {
//        // 拿到当前的下拉刷新控件，结束刷新状态
//        [weakSelf.collectionView.mj_header endRefreshing];
//        // 结束刷新
//        //[weakSelf.collectionView.mj_footer endRefreshing];
//        [SVProgressHUD showErrorWithStatus:error.domain];
//    }];
//}
//
//-(void)showData:(id)responseObject
//{
//    if (kObjectIsEmpty(responseObject))
//    {
//        return;
//    }
//
//    self.listModel2.list = [HotHouseModel mj_objectArrayWithKeyValuesArray:responseObject];
//    [self.collectionView reloadData];
//}

//轮播图广告位类型(top,middle)
//-(void)loadData3:(NSString*)type
//{
//    ZWeakSelf
//    [http_hall hallAd:type success:^(id responseObject)
//    {
//        [self.collectionView.mj_header endRefreshing];
//        [weakSelf showData3:responseObject type:type];
//    } failure:^(NSError *error) {
//        [self.collectionView.mj_header endRefreshing];
//        [SVProgressHUD showErrorWithStatus:error.domain];
//    }];
//}
//
////广告位类型(top,middle)
//-(void)showData3:(id)responseObject type:(NSString*)type
//{
//    if (kObjectIsEmpty(responseObject))
//    {
//        return;
//    }
    
//    if ([type isEqualToString:@"top"])
//    {
//        self.listModel3.list = [HotHouseModel mj_objectArrayWithKeyValuesArray:responseObject];
//
//        [self.imageUrls removeAllObjects];
//        for (int i=0; i<self.listModel3.list.count; i++)
//        {
//            HotHouseModel* ad = [self.listModel3.list objectAtIndex:i];
//            [self.imageUrls addObject:ad.image];
//        }
//    }
//    else
//    {
//        self.listModel4.list = [HotHouseModel mj_objectArrayWithKeyValuesArray:responseObject];
//
//        [self.imageUrls2 removeAllObjects];
//        for (int i=0; i<self.listModel4.list.count; i++)
//        {
//            HotHouseModel* ad = [self.listModel4.list objectAtIndex:i];
//            [self.imageUrls2 addObject:ad.image];
//        }
//    }
//
//    [self.collectionView reloadData];
    
//}

#pragma mark - <UICollectionViewDataSource>
//有多少分组
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 1;
    }
    return 8;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *gridcell = nil;
    if (indexPath.section==0)
    {
        //推荐主播
        XSJRecommendedCCell *xell = [collectionView dequeueReusableCellWithReuseIdentifier:XSJRecommendedCCellID forIndexPath:indexPath];
        //xell.listModel2 = self.listModel2;
        gridcell = xell;
    }
    else if (indexPath.section==1)
   {
       //全部直播
       XSJWholeCollectionCell *xell = [collectionView dequeueReusableCellWithReuseIdentifier:XSJWholeCollectionCelllD forIndexPath:indexPath];
       
//           YCHMyRoomModel* model = [self.listModel.list objectAtIndex:indexPath.item];
//           xell.myRoomModel = model;
       gridcell = xell;
   }
    return gridcell;
}


//分组的头部尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionReusableView *reusableview = nil;
    
    //头部
    if (kind == UICollectionElementKindSectionHeader)
    {
        if (indexPath.section == 0)
        {
            XSJHotEventTableCell *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:XSJHotEventTableCellID forIndexPath:indexPath];
            headerView.imageUrls = self.imageUrls;
            headerView.delegate = self;
            reusableview = headerView;
        }
        if (indexPath.section == 1)
        {
            XSJMoreHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:XSJMoreHeadViewID forIndexPath:indexPath];
            headerView.title = @"推荐主播";
            headerView.titleButton = @"换一换>";
            headerView.isShowButton = YES;
            headerView.delegate = self;
            reusableview = headerView;
            
        }
        
    }
    //    else if (kind == UICollectionElementKindSectionFooter)
    //    {
    //        if (indexPath.section == 0)
    //        {
    //            ZFTopLineFootView *footview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:ZFTopLineFootViewID forIndexPath:indexPath];
    //            reusableview = footview;
    //        }
    //    }
    
    return reusableview;
}

//这里我为了直观的看出每组的CGSize设置用if 后续我会用简洁的三元表示
#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        //9宫格组
        return CGSizeMake(LL_ScreenWidth , LL_ScreenWidth/5 - 5);
    }
    else if (indexPath.section == 1)
    {
        //9宫格组
        return CGSizeMake(LL_ScreenWidth/2 , LL_ScreenWidth/2 - 5);
    }
//    else if (indexPath.section == 2)
//    {
//        return CGSizeMake(LL_ScreenWidth, 120+30);
//    }
//    else if (indexPath.section == 3)
//    {
//        return CGSizeMake(LL_ScreenWidth, 120);
//    }
    return CGSizeZero;
}

//返回rect中的所有的元素的布局属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    if (indexPath.section == 4)
    {
        //        if (indexPath.row == 0)
        //        {
        //            layoutAttributes.size = CGSizeMake(ScreenW, ScreenW * 0.38);
        //        }
        //        else if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4)
        //        {
        //            layoutAttributes.size = CGSizeMake(ScreenW * 0.5, ScreenW * 0.24);
        //        }
        //        else
        //        {
        //            layoutAttributes.size = CGSizeMake(ScreenW * 0.25, ScreenW * 0.35);
        //        }
    }
    return layoutAttributes;
}

#pragma mark - head宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return CGSizeMake(LL_ScreenWidth, 180);
    }
    else if (section == 1)
    {
        return CGSizeMake(LL_ScreenWidth, 40);
    }
    return CGSizeZero;
}

#pragma mark - foot宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    //    if (section == 0)
    //    {
    //        return CGSizeMake(LL_ScreenWidth, 44);  //Top头条的宽高
    //    }
    
    return CGSizeZero;
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return (section == 5) ? 4 : 0;
}

#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return (section == 5) ? 4 : 0;
}

//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        
    }
//    else if (indexPath.section==1)
//    {
//        //k房
//        HotHouseModel* model = [self.listModel.list objectAtIndex:indexPath.row];
//        [JumpVCTool to_KtvVC:model.ID naviVC:self.naviCategoryController];
//    }
}


//头部headview被点击
//- (void)YCHHeadViewDidClickAtIndex:(NSInteger)index
//{
//    if (index>=self.listModel3.list.count)
//    {
//        return;
//    }
//
//    HotHouseModel* ad = [self.listModel3.list objectAtIndex:index];
//
//    if ([ad.title isEqualToString:@"首充奖励"])
//    {
//        YCHFirstFillHallVC* vc = [[YCHFirstFillHallVC alloc]init];
//        [self.naviCategoryController pushViewController:vc animated:YES];
//        return;
//    }
//    else if ([ad.title isEqualToString:@"分享有礼"])
//    {
//        [[JumpVCTool sharedManager] to_ShareWebVC:ad.url title:ad.title shareModel:ad naviVC:self.naviCategoryController];
//        return;
//    }
//
//    //内开
//    [JumpVCTool to_WebVC:ad.url title:ad.title naviVC:self.naviCategoryController];
//
//    //外开
//    //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:ad.url]];
//}

//YCHHotEventTableCell被点击
//- (void)YCHHotEventTableCellDidClickAtIndex:(NSInteger)index
//{
//    if (index>=self.listModel4.list.count)
//    {
//        return;
//    }
//
//    HotHouseModel* ad = [self.listModel4.list objectAtIndex:index];
//
//    if ([ad.title isEqualToString:@"首充奖励"])
//    {
//        YCHFirstFillHallVC* vc = [[YCHFirstFillHallVC alloc]init];
//        [self.naviCategoryController pushViewController:vc animated:YES];
//        return;
//    }
//    else if ([ad.title isEqualToString:@"分享有礼"])
//    {
//        [[JumpVCTool sharedManager] to_ShareWebVC:ad.url title:ad.title shareModel:ad naviVC:self.naviCategoryController];
//        return;
//    }
//
//    //内开
//    [JumpVCTool to_WebVC:ad.url title:ad.title naviVC:self.naviCategoryController];
//}
//
//
//#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //判断顶部工具View的显示和隐形
//    _topToolView.hidden = (scrollView.contentOffset.y < 0) ? YES : NO;
//
//    if (scrollView.contentOffset.y > 44)
//    {
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
//        [[NSNotificationCenter defaultCenter] postNotificationName:UserShowTopToolViewNotification object:nil];
//    }else
//    {
//        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//        [[NSNotificationCenter defaultCenter] postNotificationName:UserHideTopToolViewNotification object:nil];
//    }
}


-(void)updateTimeInVisibleCells
{
    //重新刷新section，防止闪烁
    [UIView performWithoutAnimation:^{
        [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
    }];
    
    //    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
    //    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    //    ZFHomeSpikeHeadView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ZFHomeSpikeHeadViewID forIndexPath:indexPath];
    //    if (headerView!=nil)
    //    {
    //        headerView.timeString = [self getNowTimeWithString:SpikeHeadTime];
    //    }
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.frame = CGRectMake(0, 0, LL_ScreenWidth, LL_ScreenHeight-LL_TabbarHeight-LL_StatusBarAndNavigationBarHeight);
        
        _collectionView.showsVerticalScrollIndicator = NO;
        //注册
        [_collectionView registerClass:[XSJRecommendedCCell class] forCellWithReuseIdentifier:XSJRecommendedCCellID];
        [_collectionView registerClass:[XSJWholeCollectionCell class] forCellWithReuseIdentifier:XSJWholeCollectionCelllD];
        
//        [_collectionView registerClass:[YCHHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:YCHHeadViewID];
        [_collectionView registerClass:[XSJHotEventTableCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:XSJHotEventTableCellID];
        [_collectionView registerClass:[XSJMoreHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:XSJMoreHeadViewID];
        
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

-(NSMutableArray*)imageUrls
{
    if (_imageUrls==nil) {
        _imageUrls = [[NSMutableArray alloc]init];
        [_imageUrls addObject:@"banner1"];
//        [_imageUrls addObject:@"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg"];
//        [_imageUrls addObject:@"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg"];
//        [_imageUrls addObject:@"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"];
    }
    return _imageUrls;
}

-(NSMutableArray*)imageUrls2
{
    if (_imageUrls2==nil) {
        _imageUrls2 = [[NSMutableArray alloc]init];
    }
    return _imageUrls2;
}

/**
 点击了换一换
 */
- (void)XSJMoreHeadViewDidClick
{
   // [self loadData];
}

//随机获取推荐房间
//-(void)get_room_recommend
//{
//    ZWeakSelf
//    [SVProgressHUD showWithStatus:@"正在加载"];
//    [http_user_recommend listRecommendUserByCondition:^(id responseObject)
//     {
//         [SVProgressHUD dismiss];
//         [weakSelf get_user_recommend_success:responseObject];
//
//     } failure:^(NSError *error)
//     {
//         [SVProgressHUD showErrorWithStatus:error.localizedDescription];
//     }];
//}
//
//-(void)get_room_recommend_success:(id)responseObject
//{
//    if (kObjectIsEmpty(responseObject))
//    {
//        return;
//    }
//    self.recommendUserDataList = [UserInfoModel mj_objectArrayWithKeyValuesArray:responseObject];
//
//    [self.tableView reloadData];
//}
//
//-(HotHouseListModel *)listModel
//{
//    if (_listModel==nil) {
//        _listModel = [[HotHouseListModel alloc]init];
//    }
//    return _listModel;
//}
//
//-(HotHouseListModel *)listModel2
//{
//    if (_listModel2==nil) {
//        _listModel2 = [[HotHouseListModel alloc]init];
//    }
//    return _listModel2;
//}
//
//-(HotHouseListModel *)listModel3
//{
//    if (_listModel3==nil) {
//        _listModel3 = [[HotHouseListModel alloc]init];
//    }
//    return _listModel3;
//}
//
//-(HotHouseListModel *)listModel4
//{
//    if (_listModel4==nil) {
//        _listModel4 = [[HotHouseListModel alloc]init];
//    }
//    return _listModel4;
//}

@end
